// pages/mine/repair/repair.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    untreatedList: [],//未处理的数组
    processedList: [],//处理中的数组
    completedList: [],//已完成的数组
    untreatedPage: 1,
    untreatedNoMore: false,
    processedPage: 1,
    processedNoMore: false,
    completedPage: 1,
    completedNoMore: false,

    loading:true,
    type: 0,//1：未处理 2：处理中 3：已完成
    receiveShow:false,
    repair_id:null,
    imgs:["/images/no_data_booking.png","/images/no_data_buildings.png","/images/no_data_home.png"],//测试数据

    arr: null,//子页面接收的数据
    arrpage: null,//子页面接收的page
    arrstr: "",//其他参数
    arrid:null,//子页面被领取的任务ID
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 直接加载三种类型数据
    this.getUntreatedData(true, 0)
    this.getProcessedData(true, 1)
    this.getCompletedData(true, 2)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getUntreatedData(true, 0)
    // this.getProcessedData(true, 1)
    // this.getCompletedData(true, 2)
    console.log(this.data.arr)
    console.log(this.data.type)
    if (this.data.arrstr == "needToRefresh"){
      this.getProcessedData(true,1)
      this.getCompletedData(true, 2)
    }
    // 将传回来的下一个任务插入进去并且把被领取的任务删除
    if(this.data.arr != null && this.data.arrid != null){
      let newArray = this.data.untreatedList.filter(item => {
        return item.repair_id != this.data.arrid
      })
      newArray.push(this.data.arr)
      this.setData({ untreatedList: newArray })
      // 然后刷新处理中的数据
      this.getProcessedData(true, 1)
    }
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh(e) {
    switch (this.data.type) {
      case 0:
        setTimeout(_ => {
          this.getUntreatedData(true, 0)
        }, 1000)
        break;
      case 1:
        setTimeout(_ => {
          this.getProcessedData(true, 1)
        }, 1000)
        break;
      case 2:
        setTimeout(_ => {
          this.getCompletedData(true, 2)
        }, 1000)
        break;
    }
  },
  /**
   * 上拉加载更多
   */
  onReachBottom(e) {
    switch (this.data.type) {
      case 0:
        setTimeout(_ => {
          this.getUntreatedData(false, 0)
        }, 1000)
        break;
      case 1:
        setTimeout(_ => {
          this.getProcessedData(false, 1)
        }, 1000)
        break;
      case 2:
        setTimeout(_ => {
          this.getCompletedData(false, 2)
        }, 1000)
        break;
    }
  },
  // 去详情页
  gotoPage(e){
    let option = e.currentTarget.dataset
    if(this.data.type == 0){
      wx.navigateTo({
        url: 'repairDetail/repairDetail?type=' + option.type + '&id=' + option.id + '&page=' + this.data.untreatedPage,
      })
    }else{
      wx.navigateTo({
        url: 'repairDetail/repairDetail?type=' + option.type + '&id=' + option.id,
      })
    }
  },

  // 编辑和重新提交
  editRepair(e){
    wx.navigateTo({
      url: 'repairSubmit/repairSubmit?type='+e.currentTarget.dataset.type+'&id='+e.currentTarget.dataset.id,
    })
  },
  /**
  * 切换 顶级tabs
  */
  onChange(e) {
    console.log(e.detail.index)
    this.setData({
      type: e.detail.index,
    })
  },
 
  // 领取任务
  receiveTask(e){
    this.setData({
      receiveShow: true,
      repair_id: e.currentTarget.dataset.id
    })
  },
  // 领取任务 --确定按钮
  taskConfirm(e){
    app.globalData.request("get", "applet/repair/receiveTasks", {
      id: wx.getStorageSync('id'),
      repair_id: this.data.repair_id,
      p: this.data.untreatedPage
    })
    .then(res => {
      wx.showToast({
        title: '领取成功',
      })
      this.setData({
        receiveShow:false
      })
      // 领取成功 返回新的数组
      let newArray = this.data.untreatedList.filter(item => {
        return item.repair_id != this.data.repair_id
      })
      if(res.data != null){
        newArray.push(res.data)
      }
      this.setData({ untreatedList: newArray })
      // 领取成功后要刷新处理中的数据
      this.getProcessedData(true,1)
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: "none"
      })
    })
  },
  // 领取任务 --取消
  taskCancel(){
    this.setData({
      receiveShow: false
    })
  },
  /**
   * 获取未处理列表
   */
  getUntreatedData(init, typeIndex) {
    if (init) {
      this.setData({ untreatedPage: 1, untreatedNoMore: false, untreatedList: [] })
    } else {
      this.setData({ untreatedPage: ++this.data.untreatedPage })
    }
    if (!this.data.untreatedNoMore) {
      // 后台的处理状态，1表示已处理，2表示未处理，3处理中
      let status = null;
      switch (typeIndex) {
        case 0://未处理
          status = 2;
          break;
        case 1://处理中
          status = 3;
          break;
        case 2://已完成
          status = 1;
          break;
      }
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      app.globalData.request("get", "applet/repair/staffRepair", {
        p: this.data.untreatedPage,
        status: status,
        id: wx.getStorageSync("id"),
      }).then(res => {
        if (this.data.untreatedPage == 1) {
          this.setData({
            untreatedList: res.data.data,
            untreatedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        } else {
          this.setData({
            untreatedList: [...this.data.untreatedList, ...res.data.data],
            untreatedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        }
        wx.nextTick(() => {
          wx.hideLoading()
          wx.stopPullDownRefresh()
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
        this.setData({
          loading: false
        })
        wx.hideLoading()
      })
    }
  },
  /**
   * 处理中数据
   */
  getProcessedData(init, typeIndex) {
    if (init) {
      this.setData({ processedPage: 1, processedNoMore: false, processedList: [] })
    } else {
      this.setData({ processedPage: ++this.data.processedPage })
    }
    if (!this.data.processedNoMore) {
      // 后台的处理状态，1表示已处理，2表示未处理，3处理中
      let status = null;
      switch (typeIndex) {
        case 0://未处理
          status = 2;
          break;
        case 1://处理中
          status = 3;
          break;
        case 2://已完成
          status = 1;
          break;
      }
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      app.globalData.request("get", "applet/repair/staffRepair", {
        p: this.data.processedPage,
        status: status,
        id: wx.getStorageSync("id"),
      }).then(res => {
        if (this.data.processedPage == 1) {
          this.setData({
            processedList: res.data.data,
            processedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        } else {
          this.setData({
            processedList: [...this.data.processedList, ...res.data.data],
            processedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        }
        wx.nextTick(() => {
          wx.hideLoading()
          wx.stopPullDownRefresh()
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
        this.setData({
          loading: false
        })
        wx.hideLoading()
      })
    }
  },
  /**
   * 已完成数据
   */
  getCompletedData(init, typeIndex) {
    if (init) {
      this.setData({ completedPage: 1, completedNoMore: false, completedList: [] })
    } else {
      this.setData({ completedPage: ++this.data.completedPage })
    }
    if (!this.data.completedNoMore) {
      // 后台的处理状态，1表示已处理，2表示未处理，3处理中
      let status = null;
      switch (typeIndex) {
        case 0://未处理
          status = 2;
          break;
        case 1://处理中
          status = 3;
          break;
        case 2://已完成
          status = 1;
          break;
      }
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      app.globalData.request("get", "applet/repair/staffRepair", {
        p: this.data.completedPage,
        status: status,
        id: wx.getStorageSync("id"),
      }).then(res => {
        if (this.data.completedPage == 1) {
          this.setData({
            completedList: res.data.data,
            completedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        } else {
          this.setData({
            completedList: [...this.data.completedList, ...res.data.data],
            completedNoMore: res.data.no_more,
            code: res.data.code,
            loading: false
          })
        }
        wx.nextTick(() => {
          wx.hideLoading()
          wx.stopPullDownRefresh()
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
        this.setData({
          loading: false
        })
        wx.hideLoading()
      })
    }
  },
})